package com.assistant.market.modules.market.adapter.web;

import com.assistant.core.modules.sys.domain.user.User;
import com.assistant.market.modules.market.adapter.web.model.MarketCodeInfo;
import com.assistant.market.modules.market.app.MarketCodeService;
import com.assistant.market.modules.market.app.cmd.MarketCodeInsertCmd;
import com.assistant.market.modules.market.app.cmd.MarketCodeListCmd;
import com.assistant.util.MessageBodyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/market")
public class MarketCodeController {

    @Autowired
    private MarketCodeService marketCodeService;

    @GetMapping(value = "/codes")
    public MessageBodyWrapper<List<MarketCodeInfo>> list(MarketCodeListCmd cmd) {
        return MessageBodyWrapper.success(marketCodeService.list(cmd).stream().map(MarketCodeInfo::createBy).collect(Collectors.toList()));
    }

    @PostMapping(value = "/code")
    public MessageBodyWrapper<Object> insert(@RequestBody @Validated MarketCodeInsertCmd cmd, User user) {
        marketCodeService.insert(cmd);
        return MessageBodyWrapper.success();
    }
}
